package com.hy.demo.learn.java基础类库.AutoCloseable接口;

/**
 * <p>
 * AutoCloseable接口 使用  
 * </p>
 *
 * @author cjl
 * @version 1.0
 * @since 2020-08-27 14:22
 */
public class JavaAPIDemo {

    public static void main(String[] args) throws Exception {

        // AutoCloseable自动关闭 必须结合异常处理机制来完成
        try (IMessage netMessage = new NetMessage("臭瓜皮")) {
            netMessage.send();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

interface IMessage extends AutoCloseable {
    void send();
}

class NetMessage implements IMessage {
    private String msg;

    public NetMessage(String msg) {
        this.msg = msg;
    }

    @Override
    public void send() {
        if (this.open()) {
            System.out.println("发送的消息为----" + this.msg);
        }
    }

    public Boolean open() {
        System.out.println("【OPEN】获取消息发送连接资源");
        return true;
    }

    public void close() throws Exception {
        System.out.println("【CLOSE】关闭消息连接发送通道");
    }
}
